/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.demo.widget.slice;

import com.demo.widget.ResourceTable;
import com.meis.widget.evaporate.EvaporateTextView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class AboutAbilitySlice extends AbilitySlice {
    private final String[] mSentences = {"我曾经有一个梦想", "梦想着有一天", "能够站在你的面前", "对我说", "我爱你"};
    private int mIndex = 0;
    private EvaporateTextView mEvaporateTextView;
    private EventHandler mHandler;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_about);
        mEvaporateTextView = (EvaporateTextView) findComponentById(ResourceTable.Id_etv);
        mHandler = new EventHandler(EventRunner.getMainEventRunner());
        mHandler.postTask(mAnimateTextRunnable, 500L);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mHandler != null) {
            mHandler.removeTask(mAnimateTextRunnable);
        }
    }

    private final Runnable mAnimateTextRunnable = new Runnable() {
        @Override
        public void run() {
            if (mIndex > (mSentences.length - 1)) {
                mIndex = 0;
            }
            mEvaporateTextView.animateText(mSentences[mIndex]);
            mHandler.postTask(this, 1000L);
            mIndex++;
        }
    };
}
